package com.diqizu.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.http.HttpServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.diqizu.dao.Users_Method;
import com.diqizu.entity.Users;
import com.diqizu.servlet.BaseServlet;
import com.diqizu.utli.Result;

/**
 * Servlet implementation class UsersServletAdmin
 */
@WebServlet("/admin/LoginServletAdmin")
public class LoginServletAdmin extends BaseServlet {
	Users_Method u = new Users_Method();

	/**
	 * 退出
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void doLogout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		session.removeAttribute("userName");
		session.removeAttribute("userUrl");
		System.out.println("用户名从session作用域移除了");
		String res = Result.toClient(0, "退出成功");
		req.getRequestDispatcher("../admin/login.jsp").forward(req, resp);
		PrintWriter out = resp.getWriter();
		out.write(res);
		out.flush();
		out.close();

	}

	/**
	 * 登陆
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void doLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 参数
		String res = "";
		String userName = req.getParameter("userName");
		String userPwd = req.getParameter("userPwd");
		String user1 = req.getParameter("user");
		if (user1!="") {
			res = Result.toClient(1, "验证码不正确");
			req.getRequestDispatcher("login.jsp").forward(req, resp);
			
		} else {
			System.out.println(user1);
			Users user = u.getUsers(userName, userPwd);
			System.out.println(user);
			req.setAttribute("user", user);

			if (user != null) {
				res = Result.toClient(0, "登录成功");
				HttpSession session = req.getSession();
				session.setAttribute("userName", userName);
				session.setAttribute("userUrl", user.getUserUrl());
				System.out.println("用户名存储到了session作用域");
				resp.sendRedirect("index.jsp");
			} else {

				res = Result.toClient(1, "用户名或密码不正确");
				resp.sendRedirect("login.jsp");
			}

			PrintWriter out = resp.getWriter();
			out.write(res);
			out.flush();
			out.close();

		}
	}

	/**
	 * 修改密码
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateUserPwd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.接收客户端传来的参数
		// 原密码
		String previousPwd = req.getParameter("mpass");
		System.out.println(previousPwd);
		// 新密码
		String renewpass = req.getParameter("renewpass");

		HttpSession session = req.getSession();
		String userName = (String) session.getAttribute("userName");
		// 根据用户查询所有信息
		Users users = u.Users(userName, previousPwd);

		PrintWriter out = resp.getWriter(); // 获取输出对象
		if (previousPwd.equals(users.getUserPwd())) {
			boolean res = u.updUsers(renewpass, userName);
			if (res) {
				out.write(Result.toClient(0, "修改成功"));
			} else {
				out.write(Result.toClient(1, "修改失败"));
			}
		} else {
			out.write(Result.toClient(1, "修改失败"));
		}
		session.removeAttribute("userName");
		session.removeAttribute("userUrl");
		System.out.println("用户名从session作用域移除了");

		out.flush(); // 刷新
		out.close(); // 关闭

	}
}
